/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package FrameWork;

import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author ThanhTri
 */
public class PSystem {
    protected Image m_Image;
    protected float m_XOrigin,m_YOrigin;
    
    protected Vector m_Particles;
    protected float m_EmitRate;
    protected int m_MaxParticle;
    
     public Random m_Ran;
    
     
    public PSystem(Image _Image){
        m_Image =_Image;
        m_XOrigin = 120;
        m_YOrigin = 120;
        
        m_Particles = new Vector ();
        
        m_EmitRate = 0;
        m_MaxParticle = 0;
        
        m_Ran = new Random();
    }
    public void setOrigin (float _x,float _y){
        m_XOrigin = _x;
        m_YOrigin = _y;
    }
    float RamdomFloat (float _Low,float _Hight)
{
	// bad input
	if (_Low >= _Hight)
	{
		return _Low;
	}

	float f;
	f = (m_Ran.nextInt(10000) )*(0.0001f);
	return f*(_Hight-_Low) + _Low;
}
    public void Init (){
        m_MaxParticle =5 ;
        for (int i = 0;i<m_MaxParticle;i++){
		AddParticle();
	}
    }
    public void ResetParticle (Particle _particle){
        _particle.m_X = RamdomFloat(0, 30);
        _particle.m_Y = RamdomFloat(10, 20);
        _particle.m_Vy = -1*RamdomFloat(1.0f, 50.0f);
        _particle.m_Vx = -1*RamdomFloat(-20.0f, 20.0f);
        _particle.m_LifeTime = RamdomFloat(0.2f, 0.7f);
        _particle.m_Age = 0;
        _particle.m_iLive = true;
    }
    public void AddParticle (){
        Particle p = new Particle();
        ResetParticle(p);
        m_Particles.addElement(p);
    }
    public void Reset (){
        for (int i = 0; i < m_Particles.size(); i++) {
            Particle p= (Particle)m_Particles.elementAt(i);
            ResetParticle(p);
        }
    }
    public void Update (float _Time){
        if(!m_Particles.isEmpty()){
            for (int i = 0; i < m_Particles.size(); i++) {
                Particle p = (Particle) m_Particles.elementAt(i);
                p.m_Y += p.m_Vy * _Time;
                p.m_X += p.m_Vx * _Time;
                
                p.m_Age += _Time;
                if (p.m_Age>=p.m_LifeTime){
                    ResetParticle(p);
                }
            }
        }
    }
    public void Render (Graphics _g){
        for (int i = 0; i < m_Particles.size(); i++) {
                Particle p = (Particle) m_Particles.elementAt(i);
                _g.drawRegion(m_Image, 0, 0, 4, 4, Sprite.TRANS_NONE,
                        (int)(m_XOrigin+p.m_X), (int)(m_YOrigin+p.m_Y),Graphics.TOP|Graphics.LEFT);
            }
    }
    public void DrawScreen (Graphics _g,GameScreen _Screen){
     for (int i = 0; i < m_Particles.size(); i++) {
                Particle p = (Particle) m_Particles.elementAt(i);
                _g.drawRegion(m_Image, 0, 0, 4, 4, Sprite.TRANS_NONE,
                        (int)(m_XOrigin+p.m_X)- (_Screen.getXView() - _Screen.getWidth()/2),
                        (int)(m_YOrigin+p.m_Y)- (_Screen.getYView() - _Screen.getHeight()/2),Graphics.TOP|Graphics.LEFT);
            }
    }

    public void RemoveDeadParticle (){
        
    }
}
